package com.haoran.dwz.common.util;
public class CompressEncoder {  
    final static char[] digits = {  
   		'0' , '1' , '2' , '3' , '4' , '5' ,  
        '6' , '7' , '8' , '9' , 'a' , 'b' ,  
        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,  
        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,  
        'o' , 'p' , 'q' , 'r' , 's' , 't' ,  
        'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,  
        'A' , 'B' , 'C' , 'D' , 'E' , 'F' ,  
        'G' , 'H' , 'I' , 'J' , 'K' , 'L' ,  
        'M' , 'N' , 'O' , 'P' , 'Q' , 'R' ,  
        'S' , 'T' , 'U' , 'V' , 'W' , 'X' ,  
        'Y' , 'Z' , '+' , '/' ,  
        };  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  

//    1位 	[0,63]
//    2位 	[64,4095]
//    3位          [4096,262143]
//    4位	[262144,16777215]
//    5位	[16777216,1073741823]
    	
    	System.out.println(unCompressNumber("0"));
        
    	System.out.println(unCompressNumber("/"));
        
//        System.out.println(unCompressNumber(compressNumber(999999999999999999L)));  
    } 
    
    public static String compressNumber(long number) {
     
    	return compressNumber(number,6);
	}
    
    private static String compressNumber(long number, int shift) {  
        char[] buf = new char[64];  
        int charPos = 64;  
        int radix = 1 << shift;  
        long mask = radix - 1;  
        do {  
            buf[--charPos] = digits[(int)(number & mask)];  
            number >>>= shift;  
        } while (number != 0);  
        return new String(buf, charPos, (64 - charPos));  
       }  
    
    /** 
     * 把64进制的字符串转换成10进制 
     * @param decompStr 
     * @return 
     */  
    
    public static long unCompressNumber(String decompStr)  
    {  
        long result=0;  
        for (int i =  decompStr.length()-1; i >=0; i--) {  
            if(i==decompStr.length()-1)  
            {  
                result+=getCharIndexNum(decompStr.charAt(i));  
                continue;  
            }  
            for (int j = 0; j < digits.length; j++) {  
                if(decompStr.charAt(i)==digits[j])  
                {  
                    result+=((long)j)<<6*(decompStr.length()-1-i);  
                }  
            }  
        }  
        return result;  
    }     
    /** 
     *  
     * @param ch 
     * @return 
     */  
    private static long getCharIndexNum(char ch)  
    {  
        int num=((int)ch);  
        if(num>=48&&num<=57)  //0~9
        {  
            return num-48;  //0~9
        }  
        else if(num>=97&&num<=122)  //a~z
        {  
            return num-87;  //10~35
        }else if(num>=65&&num<=90)  //A~Z
        {  
            return num-29;  //36~61
        }
        else if(num==43)  //  +
        {  
            return 62;  //62
        }  
        else if (num == 47)  // /
        {  
            return 63;  //63
        }  
        return 0;  
    }  
    
     
  
} 